package com.lagou.module3hw.hw5;

import java.util.ArrayList;

/**
 * 使用集合实现斗地主游戏的部分功能
 */
public class CardGame {

    //ArrayList集合，存放54张牌
    public static final int CARD_COUNT=54;
    private static ArrayList<String> boardCard = new ArrayList<String>();
    private static String[] card = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    private static String[] flower = {"梅花","方块","红桃","黑桃"};

    /**
     * 交换boardCard两个下标的元素
     * @param i
     * @param j
     */
    private static void swap(int i, int j){
        String s = boardCard.get(i);
        boardCard.set(i,boardCard.get(j));
        boardCard.set(j,s);
    }
    /**
     * 洗牌
     */
    public static void suffle(){
        int num = CARD_COUNT;
        //准备需要随机生成的数字
        int[] nums = new int[num];
        for(int i = 0; i < num; i++ ){
            nums[i] = i;
        }

        //存放生成的随机数
        int[] res = new int[num];

        //生成随机数
        for(int i = 0; i < res.length; i++){
            int r = (int) (Math.random() * num);    //生成随机数[0,num-1]，作为nums的下标
            res[i] = nums[r];                       //拿着需要的数据，赋值给res
            nums[r] = nums[num - 1];                //这个数据已经被拿走，用最后一个覆盖
            swap(i,res[i]);                         //交换i和res[i]的数据
            num--;                                  //num--
        }

        //System.out.println();
        //System.out.println(Arrays.toString(res));
    }

    /**
     * 打印扑克牌
     */
    public static void showCard(){
        for (String s:boardCard) {
            System.out.print(s+" ");
        }
        System.out.println();
    }

    /**
     * 初始化扑克牌
     */
    public static void initCard(){
        for ( int i = 0; i < flower.length; i++ ){
            for ( int j = 0; j < card.length; j++ ){
                boardCard.add(flower[i]+card[j]);
            }
        }
        boardCard.add("大王");
        boardCard.add("小王");
    }

    public static void main(String[] args) {

        //1.创建3个玩家
        Player player1 = new Player("玩家1");
        Player player2 = new Player("玩家2");
        Player player3 = new Player("玩家3");

        //2.初始化扑克牌
        CardGame.initCard();

        //3.洗牌
        CardGame.suffle();
        CardGame.showCard();

        //4.发牌
        for( int i = 0; i < 51; i++ ){
            if( i % 3 == 0){
                player1.setCard(CardGame.boardCard.get(i));
            }
            if( i % 3 == 1){
                player2.setCard(CardGame.boardCard.get(i));
            }
            if( i % 3 == 2){
                player3.setCard(CardGame.boardCard.get(i));
            }
        }

        //5.底牌
        ArrayList<String> diPai = new ArrayList<>();
        diPai.add(CardGame.boardCard.get(51));
        diPai.add(CardGame.boardCard.get(52));
        diPai.add(CardGame.boardCard.get(53));

        //6.查看3个玩家手中的扑克牌和底牌
        System.out.println(player1.getName() + "手中的牌是：" + player1.getCard());
        System.out.println(player2.getName() + "手中的牌是：" + player2.getCard());
        System.out.println(player3.getName() + "手中的牌是：" + player3.getCard());
        System.out.println("底牌是: " + diPai);
    }
}
